﻿package com.valeosys.navigation{
	import flash.display.*;
	import flash.geom.*;
	import flash.events.*;
	import caurina.transitions.Tweener;
	
	public class accordion extends Sprite{
		private var mywidth;
		private var myheight;
		private var navW;
		private var panelN;
		private var panelW;
		private var panelWithBtnWidth;
		private var panelH;
		private var panelWithBtnHeight;
		public var currpanel:Number;
		public static const EVENT_ON_CHANGE = "change";
		// first one is always open
		public function accordion($width:Number, $height:Number, panelNumber:Number=0, navWidth:Number=0){
			mywidth  = $width;
			myheight = $height;
			panelN = panelNumber;
			navW = navWidth;
			this.graphics.beginFill(0xFF0000);
			this.graphics.drawRect(0,0, mywidth, myheight);
			this.graphics.endFill();
			panelW = $width-panelNumber*navWidth;
			panelH = $height-panelNumber*navWidth;
			panelWithBtnWidth=panelW+navWidth;
			panelWithBtnHeight=panelH+navWidth;
			var mk:Sprite = new Sprite();
			mk.graphics.beginFill(0xFF0000);
			mk.graphics.drawRect(0,0, mywidth, myheight);
			mk.graphics.endFill();
			this.addChild(mk);
			this.mask = mk;
		}
		public function openPanel(pNumber:Number):void{
			var obj;
			var i;
			for(i=1;i<this.numChildren;i++){
				obj = this.getChildAt(i) as Sprite;
				obj.getChildByName("btn").addEventListener(MouseEvent.CLICK, handleOpenClick);
				obj.getChildByName("btn").buttonMode=true;
			}
			obj = this.getChildAt(pNumber) as Sprite;
			obj.getChildByName("btn").buttonMode=false;
			obj.getChildByName("btn").removeEventListener(MouseEvent.CLICK, handleOpenClick);
			for(i=pNumber+1; i<this.numChildren;i++){
				obj = this.getChildAt(i);
				Tweener.addTween(obj, {x:mywidth-(this.numChildren-i)*navW, time:1.0, transition:"easeOutCubic", rounded:true});
			}
			for(i=1; i<=pNumber;i++){
				obj = this.getChildAt(i);
				Tweener.addTween(obj, {x:(i-1)*navW, time:1.0, transition:"easeOutCubic", rounded:true});
			}
			currpanel=pNumber;
			dispatchEvent(new Event(EVENT_ON_CHANGE));
			
		}
		public function openVerticalPanel(pNumber:Number):void{
			var obj;
			var i;
			for(i=1;i<this.numChildren;i++){
				obj = this.getChildAt(i) as Sprite;
				obj.getChildByName("btn").addEventListener(MouseEvent.CLICK, handleOpenClick);
				obj.getChildByName("btn").buttonMode=true;
			}
			obj = this.getChildAt(pNumber) as Sprite;
			obj.getChildByName("btn").buttonMode=false;
			obj.getChildByName("btn").removeEventListener(MouseEvent.CLICK, handleOpenClick);
			for(i=pNumber+1; i<this.numChildren;i++){
				obj = this.getChildAt(i);
				Tweener.addTween(obj, {y:myheight-(this.numChildren-i)*navW, time:1.0, transition:"easeOutCubic", rounded:true});
			}
			for(i=1; i<=pNumber;i++){
				obj = this.getChildAt(i);
				Tweener.addTween(obj, {y:(i-1)*navW, time:1.0, transition:"easeOutCubic", rounded:true});
			}
			currpanel=pNumber;
			dispatchEvent(new Event(EVENT_ON_CHANGE));
			
		}
		public function addPanel(btnBackground:MovieClip,contMovie:MovieClip){
			var pnl:Sprite = new Sprite();
			var color = Math.round( Math.random()*0xFFFFFF );
			pnl.graphics.beginFill(color);
			pnl.graphics.drawRect(0,0,panelWithBtnWidth,myheight);
			pnl.graphics.endFill();
			var btn:MovieClip = getBtnBase();
			var msk:MovieClip = getBtnBase();
			pnl.addChild(btnBackground);
			pnl.addChild(btn);
			btn.addChild(msk);
			btn.mask=msk;
			var localcont = contMovie;
			pnl.addChild(localcont);
			localcont.x+=navW;
			var contmask:MovieClip = getContentBase();
			localcont.addChild(contmask);
			localcont.mask=contmask;
			this.addChild(pnl);
			pnl.x= (this.numChildren-2)*navW;
			btn.panelNumber = this.numChildren-1;
			btn.mouseChildren=false;
			btn.buttonMode=true;
			btn.name="btn";
		}
		public function addVerticalPanel(btnBackground:MovieClip,contMovie:MovieClip){
			var pnl:Sprite = new Sprite();
			var color = Math.round( Math.random()*0xFFFFFF );
			pnl.graphics.beginFill(color);
			pnl.graphics.drawRect(0,0,mywidth,panelWithBtnHeight);
			pnl.graphics.endFill();
			var btn:MovieClip = getVerticalBtnBase();
			var msk:MovieClip = getVerticalBtnBase();
			pnl.addChild(btnBackground);
			pnl.addChild(btn);
			btn.addChild(msk);
			btn.mask=msk;
			var localcont = contMovie;
			pnl.addChild(localcont);
			localcont.y+=navW;
			var contmask:MovieClip = getVerticalContentBase();
			localcont.addChild(contmask);
			localcont.mask=contmask;
			this.addChild(pnl);
			pnl.y= (this.numChildren-2)*navW;
			btn.panelNumber = this.numChildren-1;
			btn.mouseChildren=false;
			btn.buttonMode=true;
			btn.name="btn";
		}
		public function getPanelContentsAt(i):MovieClip{
			var obj:Sprite = this.getChildAt(i) as Sprite;
			return obj.getChildAt(2) as MovieClip;
			//trace(this.getChildAt(i).getChildAt(0));
		}
		private function handleOpenClick(evt:Event){
			if(evt.target.panelNumber){
				//openPanel(evt.target.panelNumber);
				openVerticalPanel(evt.target.panelNumber);
			}
		}
		private function getBtnBase():MovieClip{
			var btn:MovieClip = new MovieClip();
			var color = Math.round(Math.random()*0xFFFFFF);
			btn.graphics.beginFill(color,0);
			btn.graphics.drawRect(0,0,navW,myheight);
			btn.graphics.endFill();
			return btn;
		} 
		private function getVerticalBtnBase():MovieClip{
			var btn:MovieClip = new MovieClip();
			var color = Math.round(Math.random()*0xFFFFFF);
			btn.graphics.beginFill(color,0);
			btn.graphics.drawRect(0,0,mywidth,navW);
			btn.graphics.endFill();
			return btn;
		}
		private function getContentBase():MovieClip{
			var cont:MovieClip = new MovieClip();
			var color = Math.round(Math.random()*0xFFFFFF);
			cont.graphics.beginFill(color,0.5);
			cont.graphics.drawRect(0,0,panelW,myheight);
			cont.graphics.endFill();
			return cont;
		}
		private function getVerticalContentBase():MovieClip{
			var cont:MovieClip = new MovieClip();
			var color = Math.round(Math.random()*0xFFFFFF);
			cont.graphics.beginFill(color,0.5);
			cont.graphics.drawRect(0,0,mywidth,panelH);
			cont.graphics.endFill();
			return cont;
		}
	}
}